import { observable, action } from 'mobx';
import GraphDataApi, { SelectionType } from '../api/GraphDataApi';
import { GraphProps } from '../component/graph/graph';

const graphDataApi = new GraphDataApi();

export interface AmountData {
  name: string;
  value: number;
  amount?: number;
  parent?: number | string;
  children?: Array<AmountData>;
  date?: string;
  secondValue?: number;
}

export interface DateRange {
  startDate: string;
  endDate: string;
}

export default class GraphDataStore {

  @observable.ref graphIndex: number = 0;

  @observable.ref dateRange: DateRange;

  @observable.ref hotWordsData: Array<object> = [];

  @observable.ref caseAmountData: Array<AmountData> = [];

  @observable.ref caseTrendData: Array<AmountData> = [];

  @observable.ref caseTypeData: Array<AmountData> = [];

  @observable.ref caseCategoryData: Array<AmountData> = [];

  @observable.ref hotEventsData: Array<object> = [];

  @observable.ref caseCompletedData: Array<AmountData> = [];

  @observable.ref caseCompletedAnalysis: Array<AmountData> = [];

  @observable.ref caseAcceptedData: Array<AmountData> = [];
  
  @observable.ref caseAcceptedAnalysis: Array<AmountData> = [];

  @observable.ref hotRequestData: Array<AmountData> = [];

  @observable.ref orgHotRequestData: Array<AmountData> = [];

  @observable.ref districtAcceptedData: Array<AmountData> = [];

  @observable.ref orgAcceptedTopTenData: Array<AmountData> = [];

  @observable.ref requestTopTenData: Array<AmountData> = [];

  @observable.ref requestTypeData: Array<AmountData> = [];

  @observable.ref requestTypeTableSource: Array<AmountData> = [];
  
  @observable.ref graphList: Array<GraphProps> = [];

  @observable.ref graphSelections: SelectionType[][];

  @observable.ref hotwordData: Array<AmountData>;

  @action
  async loadDateRangeData() {
    this.dateRange = await graphDataApi.getDateRangeData();
  }

  @action
  async loadCaseAmountData(start: string, end: string) {
    this.caseAmountData = await graphDataApi.getCaseAmountGraphData(start, end);
  }

  @action
  async loadCaseTrendData(start: string, end: string) {
    this.caseTrendData = await graphDataApi.getCaseTrendGraphData(start, end);
  }

  @action
  async loadCaseTypeData(start: string, end: string) {
    this.caseTypeData = await graphDataApi.getCaseTypeGraphData(start, end);
  }

  @action
  async loadCaseCategoryData(start: string, end: string) {
    this.caseCategoryData = await graphDataApi.getCaseCategoryGraphData(start, end);
  }

  @action
  async loadHotWordsData() {
    this.hotWordsData = await graphDataApi.getHotWordsData();
  }

  @action
  async loadHotEnentsData() {
    this.hotEventsData = await graphDataApi.getHotEventsData();
  }

  @action
  async loadData() {
    await this.loadDateRangeData();
    const endDate = this.dateRange.endDate;
    const startDate = this.dateRange.startDate;
    // const startDate = endDate.replace(/\d{2}$/, '01');
    this.loadCaseAmountData(startDate, endDate);
    this.loadCaseTrendData(startDate, endDate);
    this.loadCaseTypeData(startDate, endDate);
    this.loadCaseCategoryData(startDate, endDate);
    this.loadHotWordsData();
    this.loadHotEnentsData();
  }

@action.bound
setGraphList(graphList: GraphProps[]) {
  this.graphList = graphList;
}

  @action.bound
  getCompletedData() {
    this.caseCompletedData = graphDataApi.getCompletedData();
  }
  @action.bound
  getCompletedAnalysis() {
    this.caseCompletedAnalysis = graphDataApi.getCompletedAnalysis();
  }
  @action.bound
  getAcceptedData() {
    this.caseAcceptedData = graphDataApi.getAcceptedData();
  }
  @action.bound
  getAcceptedAnalysis() {
    this.caseAcceptedAnalysis = graphDataApi.getAcceptedAnalysis();
  }
  @action.bound
  getHotRequestData() {
    this.hotRequestData = graphDataApi.getHotRequestData();
  }
  @action.bound
  getOrgHotRequestData() {
    this.orgHotRequestData = graphDataApi.getOrgHotRequestData();
  }
  @action.bound
  getGraphSelectionData() {
    this.graphSelections = graphDataApi.getGraphSelection();
  }
  @action.bound
  getHotwordData() { 
    this.hotWordsData = graphDataApi.getHotWordData();
  }
  @action.bound
  getDistrictAcceptedData() {
    this.districtAcceptedData = graphDataApi.getDistrictAcceptedData();
  }
  @action.bound
  getOrgAcceptedTopTenData() {
    this.orgAcceptedTopTenData = graphDataApi.getOrgAcceptedTopTenData();
  }
  @action.bound
  getRequestTopTenData() {
    this.requestTopTenData = graphDataApi.getRequestTopTenData();
  }
  @action.bound
  getRequestTypeData() {
    this.requestTypeData = graphDataApi.getRequestTypeData();
  }
  @action.bound
  getRequestTypeTableSource() {
    this.requestTypeTableSource = graphDataApi.getRequestTypeTableSource();
  }
}